﻿package mbalib.docviewer
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.text.*;
	import flash.ui.Keyboard;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import mbalib.ui.controls.*;
	import mbalib.ui.managers.TooltipManager;
	
	public class ToolBar extends Sprite
	{
		private var _curZoom:Number = 1;
		private var zoominBtn:Button;
		private var searchCloseBtn:Button;
		private var _autoSearchIntervalID:int;
		private var TB_HEIGHT:Number = 34;
		private var searchResultPanel:Sprite;
		private var prevBtn:Button;
		private var _pagesLoaded:uint = 0;
		private var pageGroup:Sprite;
		private var TIPS:Object=new Object();
		private var _searchKeyWord:String;
		private var searchBar:Sprite;
		private var fullBtn:Button;
		private var splitor2:Sprite;
		private var splitor3:Sprite;
		private var _pageth:uint = 0;
		private var splitor1:Sprite;
		 
		private var pagesTextField:TextField;
		private var searchResultNumTextField:TextField;
		private const BORDER_COLOR:int = 7039851;
		private const BG_DARK_COLOR:int = 13027014;
		private var nextBtn:Button;
		private const BTN_HEIGHT:Number = 23;
		private var offset:Number = 63;
	 
		private const TB_WIDTH:Number = 780;
		private var _autoSearchWaiting:Boolean = false;
		private var _searchPageLoaded:Boolean = false;
		private var _pagesAll:uint = 0;
		private var fullPrevBtn:Button;
		private var _autoSearchIndex:int = -1;
		 
		private const BG_LITE_COLOR:int = 15724527;
		private var searchGroup:Sprite;
		private var zoomoutBtn:Button;
		private var pagethTextField:TextField;
		private var doubleBtn:Button;
	 
		private var fullNextBtn:Button;
		private var _bg:Sprite;
		private var singleBtn:Button;
		private var normalBtn:Button;
		
		public function ToolBar()
		{
		 
			TIPS = {prev:"上一页", next:"下一页", zoomin:"放大", zoomout:"缩小", full:"全屏显示", normal:"恢复窗口", back:"返回普通模式", single:"单页浏览", double:"双页浏览", search:"在文档内搜索", searchp3:"在前3页搜索", searchn3:"在后3页搜索", searchclose:"取消搜索"};
			return;
		}// end function
		
		public function init() : void
		{
			var _loc_1:* = TooltipManager.getInstance();
			_loc_1.root = stage;
 			this.addBG(); 
			this.addBtns();
			this.addPageGroup();
			 
			return;
		}// end function
		
		private function fullBtnClick(event:MouseEvent = null) : void
		{
			this.changeState("TO_FULL");
			return;
		}// end function
		
		public function get pagesAll() : uint
		{
			return this._pagesAll;
		}// end function
		
		 
		
		public function get pageth() : uint
		{
			return this._pageth;
		}// end function	    
		private function zoomoutBtnClick(event:MouseEvent = null) : void
		{
			if (this._pagesLoaded < 1)
			{
				return;
			}
			if (this._curZoom > 0.5)
			{
				this._curZoom = this._curZoom - 0.25;
				this.changeZoom("TO_ZOOM");
				if (!this.zoominBtn.enabled)
				{
					this.enableBtnByName("zoominBtn");
				}
				if (this.zoomoutBtn.enabled && this._curZoom < 0.75)
				{
					this.disableBtnByName("zoomoutBtn");
				}
			}
			return;
		}// end function		
		//设置页码总数
		public function set pagesAll(param1:uint) : void
		{
			this._pagesAll = param1;
			this.pagesTextField.text = " / " + this._pagesAll;
			this.pageGroup.x = this.prevBtn.x + 24 + (this.nextBtn.x - this.prevBtn.x - 24 - this.pageGroup.width) / 2;
			this.pageGroup.visible = true;
			return;
		}// end function
		
		private function doubleBtnClick(event:MouseEvent = null) : void
		{
			dispatchEvent(new Event("TO_DOUBLE", true));
			this.disableBtnByName("doubleBtn");
			this.enableBtnByName("singleBtn");
			this.fullPrevBtn.visible = true;
			this.fullNextBtn.visible = true;
			var _loc_2:* = (parent as Reader).position;
			this.fullPrevBtn.x = _loc_2["contentX"] + 20;
			this.fullPrevBtn.y = _loc_2["contentHeight"] - 45;
			this.fullNextBtn.x = _loc_2["contentX"] + _loc_2["contentWidth"] - 65;
			this.fullNextBtn.y = _loc_2["contentHeight"] - 45;
			return;
		}// end function
		
 
		 //设置第一页
		public function set pageth(param1:uint) : void
		{
			if (this._pagesAll == 0)
			{
				this.disableAll();
				return;
			}
			this._pageth = param1;
			this.pagethTextField.text = (this._pageth + 1) + "";
			this.prevBtn.enabled = this._pageth != 0;
			this.nextBtn.enabled = this._pageth != (this._pagesAll - 1);
			this.fullPrevBtn.enabled = this._pageth != 0;
			this.fullNextBtn.enabled = this._pageth != (this._pagesAll - 1);
			 
			return;
		}// end function
		
	 
		
		private function addPageGroup() : void
		{
			this.pageGroup = new Sprite();
			var _loc_1:* = new TextFormat("Arial", 12);
			_loc_1.align = "center";
			this.pagethTextField = new TextField();
			this.pagethTextField.restrict = "0-9";
			this.pagethTextField.defaultTextFormat = _loc_1;
			this.pagethTextField.type = "input";
			this.pagethTextField.maxChars = 5;
			this.pagethTextField.border = true;
			this.pagethTextField.borderColor = 8433620;
			this.pagethTextField.background = true;
			this.pagethTextField.backgroundColor = 16777215;
			this.pagethTextField.width = 50;
			this.pagethTextField.height = 18;
			this.pagethTextField.text = "0";
			this.pageGroup.addChild(this.pagethTextField);
			this.pagethTextField.addEventListener(KeyboardEvent.KEY_UP, this.pagewordKeyUp);
			this.pagethTextField.addEventListener(FocusEvent.FOCUS_OUT, this.pagewordFocusOut);
			this.pagesTextField = new TextField();
			this.pagesTextField.defaultTextFormat = _loc_1;
			this.pagesTextField.selectable = false;
			this.pagesTextField.autoSize = "left";
			this.pagesTextField.height = 18;
			this.pagesTextField.text = " / 0";
			this.pageGroup.addChild(this.pagesTextField);
			this.pagethTextField.x = 0;
			this.pagethTextField.y = (this.TB_HEIGHT - 18) / 2;
			this.pagesTextField.x = this.pagethTextField.x + this.pagethTextField.width;
			this.pagesTextField.y = (this.TB_HEIGHT - 18) / 2;
			this.pageGroup.x = 280;
			addChild(this.pageGroup);
			this.pageGroup.visible = false;
			return;
		}// end function
		
		public function disableAll() : void
		{
			this.prevBtn.enabled = false;
			this.nextBtn.enabled = false;
			this.pagethTextField.type = "dynamic";
			this.zoominBtn.enabled = false;
			this.zoomoutBtn.enabled = false;
		 
		 
			this.fullBtn.enabled = false;
			return;
		}// end function
		
		private function pagewordFocusOut(event:FocusEvent = null) : void
		{
			this.pagethTextField.text = (this._pageth + 1) + "";
			return;
		}// end function
		
		private function nextBtnClick(event:MouseEvent = null) : void
		{
			 this.pageth ++;
			dispatchEvent(new Event("PAGETH_CHANGED", true));
			return;
		}// end function
		
		private function normalBtnClick(event:MouseEvent = null) : void
		{
			this.changeState("TO_NORMAL");
			return;
		}// end function
		
		public function set zoom(param1:Number) : void
		{
			if (param1 != this._curZoom)
			{
				this._curZoom = param1;
			}
			return;
		}// end function
		
		private function addBG() : void
		{
			if (!this._bg)
			{
				this._bg = new Sprite();
			}
			var _loc_1:* = this._bg.graphics;
			_loc_1.clear();
			_loc_1.lineStyle(1, this.BORDER_COLOR);
			var _loc_2:* = GradientType.LINEAR;
			var _loc_3:Array = [this.BG_LITE_COLOR, this.BG_DARK_COLOR];
			var _loc_4:Array = [100, 100];
			var _loc_5:Array = [0, 255];
			var _loc_6:* = new Matrix();
			new Matrix().createGradientBox((stage.stageWidth - 1), (this.TB_HEIGHT - 1), Math.PI / 2);
			_loc_1.beginGradientFill(_loc_2, _loc_3, _loc_4, _loc_5, _loc_6);
			_loc_1.drawRect(0, 0, (stage.stageWidth - 1), (this.TB_HEIGHT - 1));
			_loc_1.endFill();
			_loc_1.lineStyle(1, 16251645);
			_loc_1.drawRect(1, 1, stage.stageWidth - 3, this.TB_HEIGHT - 3);
			_loc_1.lineStyle();
			_loc_1.beginFill(0, 0.3);
			_loc_1.drawRect(0, this.TB_HEIGHT, stage.stageWidth, 2);
			_loc_1.endFill();
			this._bg.x = 0.5;
			this._bg.y = 0.5;
			addChildAt(this._bg, 0);
			return;
		}// end function
	 
		
		private function changeZoom(param1:String) : void
		{
			dispatchEvent(new Event(param1, true));
			return;
		}// end function
		
		public function enableBtnByName(param1:String) : void
		{
			var _loc_2:* = this[param1] as Button;
			if (_loc_2)
			{
				_loc_2.enabled = true;
			}
			return;
		}// end function
		
		 
		
		private function changeState(param1:String) : void
		{
			this.zoom = 1;
			this.enableBtnByName("zoomoutBtn");
			this.enableBtnByName("zoominBtn");
			dispatchEvent(new Event(param1, true));
			this.reArrange(param1);
			return;
		}// end function
		
		public function get zoom() : Number
		{
			return this._curZoom;
		}// end function
		
		public function get autoSearchIndex() : int
		{
			return this._autoSearchIndex;
		}// end function
		
	 
		
		private function addBtns() : void
		{
			var _loc_1:* = new AllSkins();
			this.prevBtn = new Button();
			this.prevBtn.setStyle("skin", _loc_1["skin_prev"]);
			this.prevBtn.x = this.offset + 250;
			this.prevBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.prevBtn);
			this.prevBtn.addEventListener(MouseEvent.CLICK, this.prevBtnClick);
			TooltipManager.getInstance().register(this.prevBtn, this.TIPS["prev"]);
			
			this.nextBtn = new Button();
			this.nextBtn.setStyle("skin", _loc_1["skin_next"]);
			this.nextBtn.x = this.offset + 370;
			this.nextBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.nextBtn);
			this.nextBtn.addEventListener(MouseEvent.CLICK, this.nextBtnClick);
			TooltipManager.getInstance().register(this.nextBtn, this.TIPS["next"]);
			
			this.zoominBtn = new Button();
			this.zoominBtn.setStyle("skin", _loc_1["skin_zoomin"]);
			this.zoominBtn.x = this.offset + 414;
			this.zoominBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.zoominBtn);
			this.zoominBtn.addEventListener(MouseEvent.CLICK, this.zoominBtnClick);
			TooltipManager.getInstance().register(this.zoominBtn, this.TIPS["zoomin"]);
			
			this.zoomoutBtn = new Button();
			this.zoomoutBtn.setStyle("skin", _loc_1["skin_zoomout"]);
			this.zoomoutBtn.x = this.offset + 442;
			this.zoomoutBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.zoomoutBtn);
			this.zoomoutBtn.addEventListener(MouseEvent.CLICK, this.zoomoutBtnClick);
			TooltipManager.getInstance().register(this.zoomoutBtn, this.TIPS["zoomout"]);
			
			this.fullBtn = new Button();
			this.fullBtn.setStyle("skin", _loc_1["skin_full"]);
			this.fullBtn.x = this.offset + 647;
			this.fullBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.fullBtn);
			this.fullBtn.addEventListener(MouseEvent.CLICK, this.fullBtnClick);
			TooltipManager.getInstance().register(this.fullBtn, this.TIPS["full"]);
			
			this.splitor1 = new Splitor();
			this.splitor1.x = this.offset + 403;
			addChild(this.splitor1);
			this.splitor2 = new Splitor();
			this.splitor2.x = this.offset + 472;
			addChild(this.splitor2);
			this.splitor3 = new Splitor();
			this.splitor3.x = this.offset + 641;
			addChild(this.splitor3);
			this.normalBtn = new Button();
			this.normalBtn.setStyle("skin", _loc_1["skin_normal"]);
			this.normalBtn.x = 10;
			this.normalBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.normalBtn);
			this.normalBtn.visible = false;
			this.normalBtn.addEventListener(MouseEvent.CLICK, this.normalBtnClick);
			TooltipManager.getInstance().register(this.normalBtn, this.TIPS["normal"]);
			this.singleBtn = new Button();
			this.singleBtn.setStyle("skin", _loc_1["skin_single"]);
			this.singleBtn.x = 10;
			this.singleBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.singleBtn);
			this.singleBtn.visible = false;
			this.singleBtn.addEventListener(MouseEvent.CLICK, this.singleBtnClick);
			TooltipManager.getInstance().register(this.singleBtn, this.TIPS["single"]);
			this.doubleBtn = new Button();
			this.doubleBtn.setStyle("skin", _loc_1["skin_double"]);
			this.doubleBtn.x = 10;
			this.doubleBtn.y = (this.TB_HEIGHT - this.BTN_HEIGHT) / 2;
			addChild(this.doubleBtn);
			this.doubleBtn.visible = false;
			this.doubleBtn.addEventListener(MouseEvent.CLICK, this.doubleBtnClick);
			TooltipManager.getInstance().register(this.doubleBtn, this.TIPS["double"]);
			this.fullPrevBtn = new Button();
			this.fullPrevBtn.setStyle("skin", _loc_1["skin_fullprev"]);
			this.fullPrevBtn.x = 20;
			this.fullPrevBtn.y = stage.stageHeight - 45;
			addChild(this.fullPrevBtn);
			this.fullPrevBtn.useHandCursor = true;
			this.fullPrevBtn.visible = false;
			this.fullPrevBtn.addEventListener(MouseEvent.CLICK, this.prevBtnClick);
			TooltipManager.getInstance().register(this.fullPrevBtn, this.TIPS["prev"]);
			this.fullNextBtn = new Button();
			this.fullNextBtn.setStyle("skin", _loc_1["skin_fullnext"]);
			this.fullNextBtn.x = stage.stageWidth - 65;
			this.fullNextBtn.y = stage.stageHeight - 45;
			addChild(this.fullNextBtn);
			this.fullNextBtn.useHandCursor = true;
			this.fullNextBtn.visible = false;
			this.fullNextBtn.addEventListener(MouseEvent.CLICK, this.nextBtnClick);
			TooltipManager.getInstance().register(this.fullNextBtn, this.TIPS["next"]);
			return;
		}// end function
		
		public function set pagesLoaded(param1:uint) : void
		{
			this._pagesLoaded = param1;
			return;
		}// end function
		
		private function pagewordKeyUp(event:KeyboardEvent = null) : void
		{
			var numInput:uint;
			var errtf:TextFormat;
			var errTips:TextField;
			var id:int;
			var event:* = event;
			var evt:* = event;
			switch(evt.keyCode)
			{
				case Keyboard.ENTER:
				case Keyboard.NUMPAD_ENTER:
				{
					if (this.pagethTextField.text == "")
					{
						this.pagethTextField.text = (this._pageth + 1) + "";
						break;
					}
					numInput = uint(this.pagethTextField.text);
					if (isNaN(numInput) || numInput < 1 || numInput > this._pagesAll)
					{
						errtf = new TextFormat("宋体", 12, 16414785);
						errTips = new TextField();
						errTips.defaultTextFormat = errtf;
						errTips.border = true;
						errTips.borderColor = 8363449;
						errTips.background = true;
						errTips.backgroundColor = 16777215;
						errTips.text = "该文档中没有页面为 " + this.pagethTextField.text + " 的页面";
						errTips.autoSize = "left";
						errTips.x = this.pagethTextField.x;
						errTips.y = this.pagethTextField.y + this.pagethTextField.height + 2;
						this.pageGroup.addChild(errTips);
						id = setInterval(function ()
						{
							clearInterval(id);
							pageGroup.removeChild(errTips);
							return;
						}// end function
							, 2000);
						this.pagethTextField.text = (this._pageth + 1) + "";
					}
					else
					{
						this.pageth = numInput - 1;
						dispatchEvent(new Event("PAGETH_CHANGED", true));
					}
				}
				default:
				{
					break;
					break;
				}
			}
			return;
		}// end function
		
		private function prevBtnClick(event:MouseEvent = null) : void
		{
			this.pageth --;
			dispatchEvent(new Event("PAGETH_CHANGED", true));
			return;
		}// end function
	 
		
		public function get pagesLoaded() : uint
		{
			return this._pagesLoaded;
		}// end function
		
		private function zoominBtnClick(event:MouseEvent = null) : void
		{
			if (this._pagesLoaded < 1)
			{
				return;
			}
			if (this._curZoom < 2)
			{
				this._curZoom = this._curZoom + 0.25;
				this.changeZoom("TO_ZOOM");
				if (!this.zoomoutBtn.enabled)
				{
					this.enableBtnByName("zoomoutBtn");
				}
				if (this.zoominBtn.enabled && this._curZoom > 1.75)
				{
					this.disableBtnByName("zoominBtn");
				}
			}
			return;
		}// end function
		
		 
		
		public function disableBtnByName(param1:String) : void
		{
			var _loc_2:* = this[param1] as Button;
			if (_loc_2)
			{
				_loc_2.enabled = false;
			}
			return;
		}// end function
		
		public function reArrange(param1:String) : void
		{
			var _loc_2:* = undefined;
			this.addBG();
			if (param1 == "TO_FULL")
			{
				this.fullBtn.visible = false;
				this.normalBtn.visible = true;
				this.normalBtn.x = stage.stageWidth - (780 - 647 - 53) - 77;
				this.splitor3.x = this.normalBtn.x - 9;
				this.searchGroup.x = this.splitor3.x - 162;
				this.splitor2.x = this.searchGroup.x - 7;
				this.zoomoutBtn.x = this.splitor2.x - 30;
				this.zoominBtn.x = this.zoomoutBtn.x - 28;
				this.splitor1.x = this.zoominBtn.x - 11;
				this.doubleBtn.visible = true;
				this.doubleBtn.x = this.splitor1.x - 30;
				this.singleBtn.visible = true;
				this.singleBtn.x = this.doubleBtn.x - 28;
				this.pageGroup.x = (stage.stageWidth - this.pageGroup.width) / 2;
				this.nextBtn.x = this.pageGroup.x + this.pageGroup.width + 6;
				this.prevBtn.x = this.pageGroup.x - 30;
				this.disableBtnByName("singleBtn");
				this.enableBtnByName("doubleBtn");
				this.fullPrevBtn.visible = true;
				this.fullNextBtn.visible = true;
				_loc_2 = (parent as Reader).position;				 
				this.fullPrevBtn.x = _loc_2["contentX"] + 20;
				this.fullPrevBtn.y = _loc_2["contentHeight"] - 45;
				this.fullNextBtn.x = _loc_2["contentX"] + _loc_2["contentWidth"] - 65;
				this.fullNextBtn.y = _loc_2["contentHeight"] - 45;
				this.pagethTextField.type = "dynamic";
				this.searchGroup.visible = false;
				this.splitor2.visible = false;
				this.splitor3.visible = false;
			}
			else
			{
				this.normalBtn.visible = false;
				this.fullBtn.visible = true;
				this.fullBtn.x = this.offset + 647;
				this.splitor3.x = this.offset + 641;
				this.searchGroup.x = this.offset + 479;
				this.splitor2.x = this.offset + 472;
				this.zoomoutBtn.x = this.offset + 442;
				this.zoominBtn.x = this.offset + 414;
				this.splitor1.x = this.offset + 403;
				this.doubleBtn.visible = false;
				this.singleBtn.visible = false;
				this.nextBtn.x = this.offset + 370;
				this.prevBtn.x = this.offset + 250;
				this.pageGroup.x = this.prevBtn.x + 24 + (this.nextBtn.x - this.prevBtn.x - 24 - this.pageGroup.width) / 2;
				this.fullPrevBtn.visible = false;
				this.fullNextBtn.visible = false;
				this.pagethTextField.type = "input";
				this.searchGroup.visible = true;
				this.splitor2.visible = true;
				this.splitor3.visible = true;
			}
			return;
		}// end function
		
		private function singleBtnClick(event:MouseEvent = null) : void
		{
			dispatchEvent(new Event("TO_SINGLE", true));
			this.disableBtnByName("singleBtn");
			this.enableBtnByName("doubleBtn");
			this.fullPrevBtn.visible = true;
			this.fullNextBtn.visible = true;
			var _loc_2:* = (parent as Reader).position;
			this.fullPrevBtn.x = _loc_2["contentX"] + 20;
			this.fullPrevBtn.y = _loc_2["contentHeight"] - 45;
			this.fullNextBtn.x = _loc_2["contentX"] + _loc_2["contentWidth"] - 65;
			this.fullNextBtn.y = _loc_2["contentHeight"] - 45;
			return;
		}// end function
		
	}
}
